package com.cn.jx.manager.controller;

import java.text.MessageFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cn.jx.common.controller.BaseController;
import com.cn.jx.common.util.jpaQuery.ConvertCondition;
import com.cn.jx.common.util.jpaQuery.PageCondition;
import com.cn.jx.common.util.jpaQuery.QueryCondition;
import com.cn.jx.common.util.page.PageObj;
import com.cn.jx.common.vo.SessionUser;
import com.cn.jx.manager.form.ResourceForm;
import com.cn.jx.manager.po.Resource;
import com.cn.jx.manager.service.mtn.IResourceServiceMtn;
import com.jx.enumType.DataGrideTypeEnum;
import com.jx.enumType.ResourceEnum;
import com.jx.model.MenuEntry;
import com.jx.util.FormValidat;
import com.jx.util.MapObject;
import com.jx.util.Response;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since
 *        <p> 创建时间：2019年12月1日
 *        <p> 功能描述：资源功能控制层
 *        <p> 官方网站：management/admin/index
 *        <p> 邮箱地址：524577360@qq.com
 * @Description 资源功能控制层
 */
@Controller
@RequestMapping("/admin/permissions/resource/")
public class ResourceController extends BaseController {

	/**
	 * 定义{@link resourceServiceMtn resourceServiceMtn <tt>定义资源</tt>}
	 */
	@Autowired
	protected IResourceServiceMtn resourceServiceMtn;
	
	/**
	 * 定义静态路径
	 */
	private static final String _PAGE_PATH="roleresource/";
	
//============================================页面跳转开头====================================================================

	/**
	 * 
	 * @Title:List
	 * @Description:TODO(页面跳转)
	 * <p>邮箱地址: 524577360@qq.com
	 * @throws
	 */
	@RequestMapping("list/{path}")
	public String List(Model model, HttpServletRequest request,@PathVariable(value = "path") String path) {
		SessionUser sessUser = getSessionUser(request);
		// 设置表头
		setModelColumns(model, DataGrideTypeEnum.RESOURCE_MGR);
		setModelPermit(model,ResourceEnum.RESOURCE_MGR,sessUser.getResourceCodeList());
		return MessageFormat.format("{0}{1}", _PAGE_PATH,"recycle".equals(path)?"resource/resourcerecycle":"resourcelist");
	}
	 
	 
	 /**
	  * 
	 * @Title:editWindow
	 * @Description:TODO(页面跳转到编辑更新页面)
	 * <p>邮箱地址: 524577360@qq.com
	 * @throws
	  */
	@RequestMapping("edit")
	public String editWindow(Model model, HttpServletRequest request, String id,String parentId,String parentname) {
		// id存在 则为修改编辑
		// parentcode 为空则增加根 否则增加子项目
		Resource res = null;
		if (Utility.isNotBlank(id)) {
			res = resourceServiceQry.getResourceById(id);
		}
		model.addAttribute("res", res);
		model.addAttribute("parentId", parentId);
		model.addAttribute("parentname", parentname);
		return _PAGE_PATH + "resource/resourceedit";
	}
	 

//============================================页面跳转结束====================================================================

//============================================资源请求开始====================================================================
	 
	/**
	 * 
	* @Title:resourceList
	* @Description:TODO(资源分页列表)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("listData/{path}")
	@ResponseBody
	public PageObj<MapObject> resourceList(Model model,@PathVariable(value = "path") String path, PageCondition page) {
		QueryCondition cond = ConvertCondition.initCondition(page, path);
		cond.setDefaultOrder("asc");// 默认规则
		cond.setDefaultSort("sort");// 默认字段
		try {
			PageObj<MapObject> pageuser = resourceServiceQry.findResourceByPageAndParams(cond);
			return pageuser;
		} catch (Exception e) {
			e.printStackTrace();
			log.debug(e.getMessage());
		}
		return null;
	}
	 
	 
	/**
	 * 
	* @Title:resourceTreeMgr
	* @Description:TODO(资源管理树列表)
	* <p>邮箱地址: 524577360@qq.com
	* @paramdEntry>
	* @throws
	 */
	@RequestMapping("listTree")
	@ResponseBody
	public List<MenuEntry> resourceTreeMgr(Model model, HttpServletRequest request, String parent) {
		List<MenuEntry> result = resourceServiceQry.findResourceTreeMgr(parent);
		return result;
	}
	 
	 
	 /**
	  * 
	 * @Title:delResource
	 * @Description:TODO(资源删除 标记删除)
	 * <p>邮箱地址: 524577360@qq.com
	 * Response
	 * @throws
	  */
	@RequestMapping("delete")
	@ResponseBody
	public Response delResource(Model model, HttpServletRequest request, String ids) {
		SessionUser sessUser = getSessionUser(request);
		try {
			// 表单验证
			FormValidat.validId(ids);
			List<String> list = JSON.parseArray(ids, String.class);
			Integer childCount = resourceServiceQry.findResourceCountByParent(list);
			if (childCount > 0) {
				return Response.Fail("您要删除的资源有子资源，请先删除子资源！");
			}
			resourceServiceMtn.delResource(list, sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	
	 /**
	  * 删除资源 物理删除 清空回收站
	  * @param model
	  * @param request
	  * @param id
	  * @return
	  */
	@RequestMapping("remove")
	@ResponseBody
	public Response removeResource(Model model, HttpServletRequest request, String ids) {
		SessionUser sessUser = getSessionUser(request);
		try {
			FormValidat.validId(ids);
			List<String> list = JSON.parseArray(ids, String.class);
			resourceServiceMtn.removeResource(list, sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	
	/**
	 * 
	* @Title:removeRole
	* @Description:TODO(找回)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("recover")
	@ResponseBody
	public Response recoverResource(Model model, HttpServletRequest request, String ids) {
		SessionUser sessUser = getSessionUser(request);
		try {
			// 表单验证
			FormValidat.validId(ids);
			// 表单验证
			List<String> list = JSON.parseArray(ids, String.class);
			resourceServiceMtn.recoverResource(list, sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	
	/**
	 * 
	* @Title:removeRole
	* @Description:TODO(移除角色 清空回收站)
	* <p>邮箱地址: 524577360@qq.com
	* @throws
	 */
	@RequestMapping("removeAll")
	@ResponseBody
	public Response removeAllResource(Model model, HttpServletRequest request) {
		SessionUser sessUser = getSessionUser(request);
		try {
			resourceServiceMtn.removeResourceAll(sessUser.getUserId());
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	}
	 
	 /**
	  * 
	 * @Title:getResourceById
	 * @Description:TODO(获取资源对象)
	 * <p>邮箱地址: 524577360@qq.com
	 * @throws
	  */
	@RequestMapping("get")
	@ResponseBody
	public Response getResourceById(Model model, HttpServletRequest request, String id) {
		try {
			Resource uf = resourceServiceQry.getResourceById(id);
			return Response.Success(uf);
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail("获取资源失败");
		}
	}
	 
	 /**
	  * 
	 * @Title:addOrUpdateResource
	 * @Description:TODO(保存或更新)
	 * <p>邮箱地址: 524577360@qq.com
	 * @throws
	  */
	 @RequestMapping("addOrUpdate")
	 @ResponseBody
	 public Response addOrUpdateResource(Model model,HttpServletRequest request,ResourceForm resourceForm) {
		SessionUser sessUser= getSessionUser(request);
		//表单验证
		 try {
			 FormValidat.exec(resourceForm);
			 resourceForm.setOperatUser(sessUser.getUserId());
			 resourceServiceMtn.saveOrUpdateResource(resourceForm);
			return Response.Success();
		} catch (Exception e) {
			log.debug(e.getMessage());
			return Response.Fail(e.getMessage());
		}
	 }
	 
//============================================资源请求结束====================================================================	 
	
}
